package com.example.task1.ui.AllService.bus;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.BusDetailsBean;
import com.example.task1.bean.BusPathBean;
import com.example.task1.bean.BusPathDetailsBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.HashMap;

import retrofit2.Call;

public class BusDetailsActivity extends BaseActivity {

    private int mId;
    private RecyclerView rvList;
    private EditText etStart;
    private EditText etEnd;
    private Button btnSubMint;
    private BusDetailsBean.DataBean mRow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_details);
        mId = getIntent().getIntExtra("id", 0);
        initView();
        initData();
    }

    @Override
    public String getMyTitle() {
        return "详情";
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/bus/line/" + mId);
        Api.send(call, BusDetailsBean.class, new CallBack<BusDetailsBean>() {
            @Override
            public void success(BusDetailsBean data) {
                mRow = data.getData();
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/bus/stop/list?linesId=" + mId);
        Api.send(apiRespCall, BusPathDetailsBean.class, new CallBack<BusPathDetailsBean>() {
            @Override
            public void success(BusPathDetailsBean data) {
                BusPathAdapter busPathAdapter = new BusPathAdapter(BusDetailsActivity.this, data.getRows());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(BusDetailsActivity.this, RecyclerView.HORIZONTAL, false);
                rvList.setLayoutManager(linearLayoutManager);
                rvList.setAdapter(busPathAdapter);
            }

            @Override
            public void error(String msg) {

            }
        });
        btnSubMint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String start = etStart.getText().toString();
                String end = etEnd.getText().toString();
                if (TextUtils.isEmpty(start)) {
                    etStart.setError("不能为空");
                } else if (TextUtils.isEmpty(end)) {
                    etEnd.setError("不能为空");
                } else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("start", start);
                    map.put("end", end);
                    map.put("price", mRow.getPrice());
                    map.put("path", mRow.getName());
                    map.put("status", 1);
                    Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/bus/order", Api.getBody(map));
                    Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                        @Override
                        public void success(ApiResp data) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(BusDetailsActivity.this);
                            builder.setTitle("确认支付订单");
                            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    HashMap<String, Object> put = new HashMap<>();
                                    put.put("orderNum", data.getOrderNum());
                                    put.put("paymentType", "电子支付");
                                    Call<ApiResp> respCall = Api.api().post(MyApplication.TOKEN, "/prod-api/api/bus/pay", Api.getBody(put));
                                    Api.send(respCall, ApiResp.class, new CallBack<ApiResp>() {
                                        @Override
                                        public void success(ApiResp data) {
                                            MyApplication.show("支付成功");
                                            finish();
                                        }

                                        @Override
                                        public void error(String msg) {

                                        }
                                    });
                                }
                            });
                            builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    finish();
                                }
                            });
                            builder.setCancelable(false);
                            builder.show();
                        }

                        @Override
                        public void error(String msg) {

                        }
                    });
                }
            }
        });
    }

    private void initView() {
        rvList = findViewById(R.id.rv_list);
        etStart = findViewById(R.id.et_start);
        etEnd = findViewById(R.id.et_end);
        btnSubMint = findViewById(R.id.btn_subMint);
    }
}